package psn.hui.bus.service.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import psn.hui.bus.domain.Car;
import psn.hui.bus.mapper.CarMapper;
import psn.hui.bus.service.CarService;
import psn.hui.bus.vo.CarVo;
import psn.hui.sys.constant.SysConstant;
import psn.hui.sys.utils.AppFileUtils;
import psn.hui.sys.utils.DataGridView;

/**
 * 	车辆管理服务接口实现类
 * 
 * @author hui
 *
 */
@Service
public class CarServiceImpl implements CarService {

	@Autowired
	private CarMapper carMapper;

	/**
	 * 	插入车辆数据，动态sql(可选字段)
	 * 
	 * @param car
	 * @return
	 */
	@Override
	public int addCar(Car car) {
		car.setCreatetime(new Date());
		car.setCarimg(AppFileUtils.removeTempFileSuffix(car.getCarimg(),SysConstant.FILE_UPLOAD_TEMP_SUFFIX));
		return carMapper.insertSelective(car);
	}

	/**
	 * 	根据主键(carnumber)删除车辆
	 * 
	 * @param carnumber
	 * @return
	 */
	@Override
	public int deleteByCarnumber(String carnumber) {
		//将该车牌号对应的旧文件转变为临时文件
		String oldFileName = carMapper.getCarimgByCarnumber(carnumber);
		int rs = carMapper.deleteByPrimaryKey(carnumber);
		if(rs > 0) {
			try {
				//将相关图片转为临时文件（定式清理）
				AppFileUtils.addTempFileSuffix(oldFileName, SysConstant.FILE_UPLOAD_TEMP_SUFFIX);
			} catch (Exception e) {
			}
		}
		return rs;
	}

	/**
	 * 根据主键(carnumber)更新车辆数据，动态sql(可选字段)
	 * 
	 * @param car
	 * @return
	 */
	@Override
	public int updateByCarnumber(Car car) {
		//将该车牌号对应的旧文件转变为临时文件
		String oldFileName = carMapper.getCarimgByCarnumber(car.getCarnumber());
		String newImg = car.getCarimg();
		//若图片路径有修改
		boolean flag = oldFileName!=null && newImg!=null && !oldFileName.equals(newImg);
		if(flag) {
			//有修改说明新的图片为临时文件，要去除临时文件后缀
			car.setCarimg(AppFileUtils.removeTempFileSuffix(car.getCarimg(),SysConstant.FILE_UPLOAD_TEMP_SUFFIX));
		}
		int rs = carMapper.updateByPrimaryKeySelective(car);
		if(flag && rs > 0) {
			try {
				//图片有修改且修改数据库成功，将旧的图片变为临时文件
				AppFileUtils.addTempFileSuffix(oldFileName,SysConstant.FILE_UPLOAD_TEMP_SUFFIX);
			} catch (Exception e) {
			}
		}
		return rs;
	}

	/**
	 * 根据主键(carnumber)查询车辆
	 * 
	 * @param carnumber
	 * @return
	 */
	@Override
	public Car selectByCarnumber(String carnumber) {
		return carMapper.selectByPrimaryKey(carnumber);
	}

	/**
	 * 根据可选字段查询车辆
	 * 
	 * @param carVo
	 * @return
	 */
	@Override
	public DataGridView queryAllCar(CarVo carVo) {
		Page<Object> page = PageHelper.startPage(carVo.getPage(), carVo.getLimit());
		List<Car> cars = carMapper.queryAllCar(carVo);
		return new DataGridView(page.getTotal(), cars);
	}
	/**
	 * 	根据carnumber批量删除车辆数据
	 * @param carnumbers
	 * @return
	 */
	public int batchDeleteCar(String[] carnumbers) {
		int rs = 0;
		for (String carnumber : carnumbers) {
			//将该车牌号对应的旧文件转变为临时文件
			String oldFileName = carMapper.getCarimgByCarnumber(carnumber);
			int rs2 = carMapper.deleteByPrimaryKey(carnumber);
			if(rs2 > 0) {
				try {
					//将相关图片转为临时文件（定式清理）
					AppFileUtils.addTempFileSuffix(oldFileName, SysConstant.FILE_UPLOAD_TEMP_SUFFIX);
				} catch (Exception e) {
				}
			}
			rs += rs2;
		}
		return rs;
	}
	/**
	 * 	根据carnumber查询carimg(用作删除时清除图片用)
	 * @param carnumber
	 * @return 
	 */
	@Override
	public String getCarimgByCarnumber(String carnumber) {
		return carMapper.getCarimgByCarnumber(carnumber);
	}
}